package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.MaterialType;
import com.mt.roll.core.dal.entity.StorageBox;
import com.mt.roll.core.dal.entity.StorageBoxCircInfo;
import com.mt.roll.core.dal.service.StorageBoxCircInfoServiceBiz;
import com.mt.roll.core.dal.service.StorageBoxServiceBiz;
import com.mt.roll.core.enums.WhetherEnum;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.mapper.StorageBoxCircInfoVoMapper;
import com.mt.roll.wms.params.StorageBoxCircInfoParam;
import com.mt.roll.wms.params.StorageBoxCircInfoQueryParam;
import com.mt.roll.wms.service.StorageBoxCircInfoService;
import com.mt.roll.wms.service.StorageBoxService;
import com.mt.roll.wms.vo.StorageBoxCircInfoVo;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @auther why
 * @date 2021/9/26 14:42
 */
@Service
@Transactional
public class StorageBoxCircInfoServiceImpl extends BaseService implements StorageBoxCircInfoService {

    @Autowired
    private StorageBoxCircInfoVoMapper storageBoxCircInfoVoMapper;

    @Autowired
    private StorageBoxCircInfoServiceBiz storageBoxCircInfoServiceBiz;

    @Autowired
    private StorageBoxServiceBiz storageBoxServiceBiz;


    @Override
    public R<PageVo<StorageBoxCircInfoVo>> page(StorageBoxCircInfoQueryParam storageBoxCircInfoQueryParam) {
        IPage<StorageBoxCircInfoVo> storageBoxCircInfoVoIPage = new Page<>(storageBoxCircInfoQueryParam.getCurrent(),storageBoxCircInfoQueryParam.getSize());
        IPage<StorageBoxCircInfoVo> page = storageBoxCircInfoVoMapper.page(storageBoxCircInfoVoIPage, storageBoxCircInfoQueryParam);
        return successful(new PageVo<>(page,StorageBoxCircInfoVo.class));
    }

    @Override
    public R<IdVo> add(StorageBoxCircInfoParam storageBoxCircInfoParam) {
        QueryWrapper<StorageBoxCircInfo> wrapper = new QueryWrapper<>();
        wrapper.eq(StorageBoxCircInfo.CURR_LOCATION_ID, storageBoxCircInfoParam.getCurrLocationId());
        Assert.eqZero(storageBoxCircInfoServiceBiz.count(wrapper), "该库位已经存在，请勿重复添加");

        QueryWrapper<StorageBoxCircInfo> storageBoxIdWrapper = new QueryWrapper<>();
        storageBoxIdWrapper.eq(StorageBoxCircInfo.STORAGE_BOX_ID, storageBoxCircInfoParam.getStorageBoxId());
        Assert.eqZero(storageBoxCircInfoServiceBiz.count(storageBoxIdWrapper), "该存储箱已经存放在库位上，请先操作在添加");

        StorageBoxCircInfo storageBoxCircInfo = new StorageBoxCircInfo();
        BeanUtils.copyProperties(storageBoxCircInfoParam, storageBoxCircInfo);
        setCommonField(storageBoxCircInfo);
        storageBoxCircInfoServiceBiz.save(storageBoxCircInfo);
        return successful(IdVo.builder().id(storageBoxCircInfo.getId()).build());
    }

    @Override
    public R<IdVo> update(StorageBoxCircInfoParam storageBoxCircInfoParam) {
        StorageBoxCircInfo storageBoxCircInfo = storageBoxCircInfoServiceBiz.getById(storageBoxCircInfoParam.getId());
        //修改信息需要检验是否已存在
        if (!storageBoxCircInfo.getCurrLocationId().equals(storageBoxCircInfoParam.getCurrLocationId())) {
            QueryWrapper<StorageBoxCircInfo> wrapper = new QueryWrapper<>();
            wrapper.eq(StorageBoxCircInfo.CURR_LOCATION_ID, storageBoxCircInfoParam.getCurrLocationId());
            Assert.eqZero(storageBoxCircInfoServiceBiz.count(wrapper), "该库位已经存在");
        }
        if (!storageBoxCircInfo.getStorageBoxId().equals(storageBoxCircInfoParam.getStorageBoxId())) {
            QueryWrapper<StorageBoxCircInfo> wrapper = new QueryWrapper<>();
            wrapper.eq(StorageBoxCircInfo.STORAGE_BOX_ID, storageBoxCircInfoParam.getStorageBoxId());
            Assert.eqZero(storageBoxCircInfoServiceBiz.count(wrapper), "该存储箱已经上架");
        }
        StorageBoxCircInfo updateStorageBoxCircInfo = new StorageBoxCircInfo();
        BeanUtils.copyProperties(storageBoxCircInfoParam, updateStorageBoxCircInfo);
        setUpdateCommonField(updateStorageBoxCircInfo);
        storageBoxCircInfoServiceBiz.updateById(updateStorageBoxCircInfo);
        return successful(IdVo.builder().id(storageBoxCircInfo.getId()).build());
    }

    @Override
    public R<PageVo<StorageBoxCircInfoVo>> pageForBind(StorageBoxCircInfoQueryParam storageBoxCircInfoQueryParam) {
        QueryWrapper<StorageBoxCircInfo> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotNull(storageBoxCircInfoQueryParam.getStorageBoxId()), StorageBoxCircInfo.STORAGE_BOX_ID, storageBoxCircInfoQueryParam.getStorageBoxId())
                .eq(ObjectUtils.isNotNull(storageBoxCircInfoQueryParam.getCurrLocationId()), StorageBoxCircInfo.CURR_LOCATION_ID, storageBoxCircInfoQueryParam.getCurrLocationId())
                .orderByDesc(StorageBoxCircInfo.ID);
        IPage<StorageBoxCircInfo> page = storageBoxCircInfoServiceBiz.page(new Page<>(storageBoxCircInfoQueryParam.getCurrent(), storageBoxCircInfoQueryParam.getSize()), wrapper);
        return successful(new PageVo<>(page, StorageBoxCircInfoVo.class));
    }

    @Override
    public R<StorageBoxCircInfoVo> get(IdParam idParam) {
        StorageBoxCircInfo storageBoxCircInfo = storageBoxCircInfoServiceBiz.getById(idParam.getId());
        Assert.notNull("存储箱上架信息不存在", storageBoxCircInfo);
        StorageBoxCircInfoVo storageBoxCircInfoVo = StorageBoxCircInfoVo.builder().build();
        BeanUtils.copyProperties(storageBoxCircInfo, storageBoxCircInfoVo);
        return successful(storageBoxCircInfoVo);
    }
    @Override
    public R<StorageBoxCircInfoVo> getByLocationId(IdParam idParam) {
        QueryWrapper<StorageBoxCircInfo> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotNull(idParam.getId()), StorageBoxCircInfo.CURR_LOCATION_ID, idParam.getId());
        StorageBoxCircInfo one = storageBoxCircInfoServiceBiz.getOne(wrapper);
        Assert.notNull("该库位未被初始化，该库位上无箱子", one);
        StorageBoxCircInfoVo storageBoxCircInfoVo = StorageBoxCircInfoVo.builder().build();

        // 到时候这段删除   -- 开始
        Long storageBoxId = one.getStorageBoxId();
        StorageBox byId = storageBoxServiceBiz.getById(storageBoxId);
        if(byId!=null && byId.getCode()!=null){
            storageBoxCircInfoVo.setStorageBoxCode(byId.getCode());
        }
        // 到时候这段删除   -- 结束

        BeanUtils.copyProperties(one, storageBoxCircInfoVo);
        return successful(storageBoxCircInfoVo);
    }

    @Override
    public R<IdVo> delete(IdParam idParam) {
        StorageBoxCircInfo storageBoxCircInfo = storageBoxCircInfoServiceBiz.getById(idParam.getId());
        if (storageBoxCircInfo == null) {
            return failed("当前库存存储箱信息不存在");
        }
        //使用更新方式删除，需要把code设置为历史code（后面跟随机数）
        StorageBoxCircInfo updateStorageBoxCircInfo = new StorageBoxCircInfo().setId(storageBoxCircInfo.getId());
        updateStorageBoxCircInfo.setCode(storageBoxCircInfo.getCode() + "_" + RandomUtils.nextInt());
        updateStorageBoxCircInfo.setValid(WhetherEnum.NO.getValue());
        setUpdateCommonField(updateStorageBoxCircInfo);
        storageBoxCircInfoServiceBiz.updateById(updateStorageBoxCircInfo);
        //更新时逻辑删除字段没有更新，需要再调用一次删除操作
        storageBoxCircInfoServiceBiz.removeById(updateStorageBoxCircInfo.getId());
        return successful(IdVo.builder().id(storageBoxCircInfo.getId()).build());
    }
}
